package com.itheima.book.controller;

import com.itheima.book.domain.Book;
import com.itheima.book.service.BookService;

import java.util.Scanner;

public abstract class BookBaseController {
    BookService bookService = new BookService();
    Scanner sc = new Scanner(System.in);


    public void start() {
        while (true) {

            printView();

            user();
        }
    }


    public void user() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的选择: ");
        String next = sc.next();

        switch (next) {
            case "1":
                inputBookInfo();
                break;
            case "2":
                deleteByBookId();
                break;
            case "3":
                updateBookByBookId();
                break;
            case "4":
                findAll();
                break;
            case "5":
                System.out.println("欢迎下次使用");
                System.exit(0);
            default:
                System.out.println("您输入的数据有误，请重新输入: ");
                break;
        }
    }





    public void updateBookByBookId(){
       String bookId = "";
       while (true) {
           System.out.println("请输入id: ");
            bookId = sc.next();

           boolean a = bookService.isExistsBookId(bookId);
           if (a) {
               break;
           } else {
               System.out.println("id不存在，请重新输入");
           }
       }
           System.out.println("请输入名称: ");
           String bookName = sc.next();
           System.out.println("请输入发布时间: ");
           String publishDate = sc.next();
           System.out.println("请输入价格: ");
           String price = sc.next();

           // 将图书信息封装到Book对象中，调用BookService提供的方法(addBook)，把Book对象作为
           //参数传递过去
           Book book = new Book();
           book.setBookId(bookId);
           book.setBookName(bookName);
           book.setPublishDate(publishDate);
           book.setPrice(price);

           bookService.updateBookByBookId(book);

               System.out.println("修改成功");




    }


    public void deleteByBookId(){

        while (true) {
            System.out.println("请输入id");
            String next = sc.next();

            boolean a = bookService.isExistsBookId(next);
            if (a) {
                bookService.deleteByBookId(next);
                System.out.println("删除成功");
                break;
            } else {
                System.out.println("输入错误，请重新输入");
            }
        }
    }



    public void findAll(){
        Book[] all = bookService.findAll();
        System.out.println("图书编号\t\t\t书名\t\t\t上架时间\t\t\t价格");
        for (int i = 0; i < all.length; i++) {
            Book away = all[i];
            if (away != null) {
                System.out.println(away.getBookId() + "\t\t\t" + away.getBookName() + "\t\t\t" + away.getPublishDate() + "\t\t\t" + away.getPrice());
            }
        }
    }

    public void inputBookInfo() {

        String bookId = "";
        while (true) {
            // 接收键盘录入数据(bookId , bookName,publishDate,price)

            System.out.println("请输入id: ");
             bookId = sc.next();

            //在录入bookId的时候，需要调用service层的isExistsBookId方法
            // 判断录入的bookId是否存在，如果存在就继续录入；如果不存在就结束录入
            boolean a = bookService.isExistsBookId(bookId);
            if (a) {
                System.out.println("id重复，请重新输入");
            } else {
                break;
            }
        }


        System.out.println("请输入名称: ");
        String bookName = sc.next();
        System.out.println("请输入发布时间: ");
        String publishDate = sc.next();
        System.out.println("请输入价格: ");
        String price = sc.next();

        // 将图书信息封装到Book对象中，调用BookService提供的方法(addBook)，把Book对象作为
        //参数传递过去
        Book book = new Book();
        book.setBookId(bookId);
        book.setBookName(bookName);
        book.setPublishDate(publishDate);
        book.setPrice(price);


        boolean b = bookService.addBook(book);
        if(b){
            System.out.println("输入成功");
        }else {
            System.out.println("输入失败");
        }

    }

    public abstract void printView();

}
